var express = require('express');
const router = express.Router();
//加密规则
const md5 = require('md5')
//中间件
const checkTokenMiddleWare = require('../../MiddleWares/checkTokenMiddleWare')
const resultResponeMiddleWare = require('../../MiddleWares/resultResponeMiddleWare');
//文档模型
const UserModel = require('../../model/UserModel');
//token包
const jwt = require('jsonwebtoken')
const { secret } = require('../../config/config');
//校验包
// 导入 @escook/express-joi
const expressJoi = require('@escook/express-joi')
//校验对象
const update_user = require('../../schema/update_user')

//获取用户信息
router.get('/userinfo', checkTokenMiddleWare, resultResponeMiddleWare, function (req, res, next) {
    UserModel.findById(req.decoded.id).then(data => {
        console.log(data._id);
        res.sendResponse(0, "获取用户信息成功", data)
    }).catch(err => {
        console.log(err);
        res.sendResponse(1, "获取用户信息失败")
    })
});

//更新用户信息
router.post('/userinfo', checkTokenMiddleWare, resultResponeMiddleWare, (req, res, next) => {
    // console.log(req.body);
    let { nickname, email } = req.body
    let { id } = req.decoded
    if (!nickname || !email) {
        return res.sendResponse(1, "请输入用户信息")
    }
    // console.log(req);
    UserModel.findOneAndUpdate({ _id: id }, { nickname, email }, { new: true, select: '-password' }).then(data => {
        // console.log(data);
        if (data) {
            return res.sendResponse(0, "更新成功", data)
        }
        res.sendResponse(1, "更新失败")
    }).catch(err => {
        res.sendResponse(1, err)
    })
})

//重置用户密码
router.post('/updatepwd', checkTokenMiddleWare, resultResponeMiddleWare, (req, res, next) => {
    console.log(req.body);
    let { oldPwd, newPwd } = req.body
    let { id } = req.decoded
    //验证密码
    if (newPwd === oldPwd) {
        return res.sendResponse(1, "新旧密码相同")
    }
    UserModel.findOne({ _id: id, password: md5(oldPwd) }).then(user => {
        if (!user) {
            return res.sendResponse(1, "旧密码不正确")
        }
        // console.log(data);
        UserModel.updateOne({ _id: id }, { password: md5(newPwd) }).then(result => {
            if (result.modifiedCount === 1) {
                return res.sendResponse(0, "修改密码成功")
            }
        }).catch(err => {
            return res.sendResponse(1, "修改密码失败")
        })
    }).catch(err => {
        return res.sendResponse(1, err)
    })
})

//更新用户头像
router.post('/update/avatar', checkTokenMiddleWare, resultResponeMiddleWare, (req, res, next) => {
    console.log(req.decoded);
    // const fs = require('fs');
    // const path = __dirname+'/'+ Date.now() +'.png';
    // const base64 = req.body.userpic.replace(/^data:image\/\w+;base64,/, "");//去掉图片base64码前面部分data:image/png;base64
    // const dataBuffer = Buffer.from(base64, 'base64'); //把base64码转成buffer对象，
    // fs.writeFile("./public/images/img.png", dataBuffer, function(err){//用fs写入文件
    //     if(err){
    //         console.log(err);
    //         res.sendResponse(1,"fail")
    //     }else{
    //         console.log('写入成功！');
    //         res.sendResponse(0,"ok")
    //     }
    // })
    let { user_pic } = req.body
    let { id } = req.decoded
    UserModel.findOne({ _id: id }).then(data => {
        if (!data) return res.sendResponse(1, "上传失败")
        UserModel.updateOne({ _id: id }, { user_pic }).then(data => {
            // console.log(data);
            res.sendResponse(0, "ok")
        }).catch(err => {
            res.sendResponse(1, "上传头像失败")
        })
    }).catch(err => {
        res.sendResponse(1, err)
    })

})






module.exports = router;
